/**
 * 菜单工具函数
 * 用于获取菜单数据
 */

/**
 * 获取菜单项列表
 * 从aiTemp/loveHeart目录下获取所有HTML文件作为菜单项
 * @returns {Promise<Array>} 菜单项数组
 */
export const fetchMenuItems = async () => {
  try {
    // 这里模拟从aiTemp/loveHeart目录获取HTML文件列表
    // 在实际应用中，可以通过API或其他方式获取文件列表
    const menuItems = [
      { name: 'claude3.7 sonnet + cursor提示词', url: '/loveHeart/index-claude-test.html', star: 5 },
      { name: 'claude3.7 sonnet + 蝴蝶动效', url: '/loveHeart/index-claude-hudie.html', star: 3 },
      { name: 'claude3.7 sonnet + 气泡动效', url: '/loveHeart/index-claude-qipao.html', star: 3 },
      
      { name: 'claude3.7 sonnet', url: '/loveHeart/index-claude.html', star: 4 },
      { name: 'gemini + deepsite', url: '/loveHeart/index-gemini-deepsite.html', star: 3 },
      { name: 'deepsite', url: '/loveHeart/index-deepsite.html', star: 4 },
      { name: 'gemini 2.0 flash (2)', url: '/loveHeart/index-gemini-v2.html', star: 2 },
      { name: 'gemini 2.0 flash (1)', url: '/loveHeart/index-gemini.html', star: 2 },
      { name: 'gpto1', url: '/loveHeart/index-gpto1.html', star: 3 },
      { name: 'grok3', url: '/loveHeart/index-grok3.html', star: 3 },
      { name: 'mgx.dev', url: '/loveHeart/index-mgx.html', star: 5 },
      { name: 'qwen3', url: '/loveHeart/index-qwen.html', star: 1 },
      { name: 'v0.dev', url: '/loveHeart/index-v0.html', star: 5 },
    ];

    return menuItems;
  } catch (error) {
    console.error('获取菜单数据失败:', error);
    return [];
  }
};

/**
 * 根据URL参数获取初始菜单项
 * @returns {Object|null} 菜单项或null
 */
export const getInitialMenuItem = (menuItems) => {
  if (!menuItems || menuItems.length === 0) {
    return null;
  }

  // 从URL参数中获取指定的菜单项
  const urlParams = new URLSearchParams(window.location.search);
  const urlParam = urlParams.get('url');

  if (urlParam) {
    // 查找匹配的菜单项
    const matchedItem = menuItems.find(item => {
      return item.url.includes(urlParam);
    });

    if (matchedItem) {
      return matchedItem;
    }
  }

  // 如果没有匹配项，返回第一个菜单项
  return menuItems[0];
};